'use strict';

/**
 * [getGender 根据身份证号码获取性别]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @return   {[type]}   [description]
 */
exports.getGender = function(idcardno = ''){
	let sex = "";
	try{
		if(idcardno.length == 15 ){
			if(parseInt(idcardno.charAt(14)/2)*2!=idcardno.charAt(14)){
				sex="男";
			} else {
				sex="女";
			}
		}else if(idcardno.length == 18){
			if(parseInt(idcardno.charAt(16)/2)*2!=idcardno.charAt(16)){
				sex="男";
			} else {
				sex="女";
			}
		}	
	} catch(err){
		console.error(err.message);
	} 
	return sex;
}
/**
 * [getBorn 根据身份证号获取生日]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   idcarno [description]
 * @return   {[type]}           [description]
 */
exports.getBorn = function(idcardno){
	let birthday = "";
	try{
		if(idcardno.length == 15 ){
			birthday = idcardno.charAt(6)+idcardno.charAt(7);
			if(parseInt(birthday)<10){
				birthday = '20'+birthday;
			} else {
				birthday = '19'+birthday;
			}
			birthday=birthday+'-'+idcardno.charAt(8)+idcardno.charAt(9)+'-'+idcardno.charAt(10)+idcardno.charAt(11);
		} else if(idcardno.length == 18 ){
			birthday=idcardno.charAt(6)+idcardno.charAt(7)+idcardno.charAt(8)+idcardno.charAt(9)+'-'+idcardno.charAt(10)+idcardno.charAt(11)+'-'+idcardno.charAt(12)+idcardno.charAt(13);
		}		
	} catch(err){
		console.error(err.message);
	} 
	return birthday;
}
/**
 * [getAge 根据身份证号获取年龄]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   idcardno [description]
 * @return   {[type]}            [description]
 */
exports.getAge = function(idcardno,newDay,newMonth){
	let returnAge = 0;
	try{
		let born 			= this.getBorn(idcardno);
		let strBirthdayArr 	= born.split("-");
		let birthYear      	= strBirthdayArr[0];
		let birthMonth     	= strBirthdayArr[1];
		let birthDay       	= strBirthdayArr[2];
		let d 				= new Date();
		let nowYear 		= d.getFullYear();
		let nowMonth 		= d.getMonth() + 1;
		let nowDay 			= d.getDate();
		newDay 				= newDay || nowDay;
		newMonth 			= newMonth || nowMonth;
		if(nowYear == birthYear){
		    returnAge = 0;//同年 则为0岁
		} else {
		    let ageDiff = nowYear - birthYear ; //年之差
		    if(ageDiff > 0){
		        if(newMonth == birthMonth) {
		            let dayDiff = newDay - birthDay;//日之差
		            if(dayDiff < 0){
		                returnAge = ageDiff - 1;
		            } else {
		                returnAge = ageDiff ;
		            }
		        } else {
		            let monthDiff = newMonth - birthMonth;//月之差
		            if(monthDiff < 0) {
		                returnAge = ageDiff - 1;
		            } else {
		                returnAge = ageDiff;
		            }
		        }
		    } else {
		        returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
		    }
		}
	} catch(err){
		console.error(err.message);
	}
	return returnAge;//返回周岁年龄
}
/**
 * [getStarsign 根据身份证号获取星座]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   idcardno [description]
 * @return   {[type]}            [description]
 */
exports.getStarsign = function(idcardno){
	let strValue = '';
	try{
		let born = this.getBorn(idcardno);
		if(born == "") return starsign;
		let date = new Date(born);
		let month = date.getMonth() + 1;
		let day = date.getDate();
		if((month == 1 && day <= 21) || (month == 2 && day <= 19)){
			strValue = '水瓶座';
		}else if((month == 2 && day > 20) || (month == 3 && day <= 20)) {
        	strValue = "双鱼座";
	    }else if ((month == 3 && day > 20) || (month == 4 && day <= 20)) {
	        strValue = "白羊座";
	    }else if ((month == 4 && day > 20) || (month == 5 && day <= 21)) {
	        strValue = "金牛座";
	    }else if ((month == 5 && day > 21) || (month == 6 && day <= 21)) {
	        strValue = "双子座";
	    }else if ((month == 6 && day > 21) || (month == 7 && day <= 22)) {
	        strValue = "巨蟹座";
	    }else if ((month == 7 && day > 22) || (month == 8 && day <= 23)) {
	        strValue = "狮子座";
	    }else if ((month == 8 && day > 23) || (month == 9 && day <= 23)) {
	        strValue = "处女座";
	    }else if ((month == 9 && day > 23) || (month == 10 && day <= 23)) {
	        strValue = "天秤座";
	    }else if ((month == 10 && day > 23) || (month == 11 && day <= 22)) {
	        strValue = "天蝎座";
	    }else if ((month == 11 && day > 22) || (month == 12 && day <= 21)) {
	        strValue = "射手座";
	    }else if ((month == 12 && day > 21) || (month == 1 && day <= 20)) {
	        strValue = "魔羯座";
	    } 
	} catch(err){
		console.error(err.message);
	}
	return strValue;
}
/**
 * [getZodiac 根据身份证号码获取生肖]
 * @Author   szjcomo
 * @DateTime 2019-09-21
 * @param    {[type]}   idcardno [description]
 * @return   {[type]}            [description]
 */
exports.getZodiac = function(idcardno){
	let val = '';
	try{
		let start = '1901';
		let born = this.getBorn(idcardno);
		if(born == "") return val;
		let end = new Date(born).getFullYear();
		let x = (start - end) % 12;
	    if (x == 1 	|| x == -11) 	val = '鼠';
	    if (x == 0)              	val = '牛';
	    if (x == 11 || x == -1) 	val = '虎';
	    if (x == 10 || x == -2) 	val = '兔';
	    if (x == 9 	|| x == -3)  	val = '龙';
	    if (x == 8 	|| x == -4)  	val = '蛇';
	    if (x == 7 	|| x == -5)  	val = '马';
	    if (x == 6 	|| x == -6)  	val = '羊';
	    if (x == 5 	|| x == -7)  	val = '猴';
	    if (x == 4 	|| x == -8)  	val = '鸡';
	    if (x == 3 	|| x == -9)  	val = '狗';
	    if (x == 2 	|| x == -10) 	val = '猪';
	} catch(err){
		console.error(err.message);
	}
	return val;
}
/**
 * [repairIdentity 身份证号码自动补全]
 * @Author   szjcomo
 * @DateTime 2019-09-21
 * @param    {[type]}   idcardno [description]
 * @return   {[type]}            [description]
 */
exports.repairIdentity = function(idcardno){
	try{
		if(idcardno.length == 17){
			let str = idcardno.split('');
			let sum = 0;;
			let xs = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
			str.forEach((item,index) => {
				sum+=item * xs[index];
			})
			let dy = [1,0,'X',9,8,7,6,5,4,3,2];
			if(dy[sum%11]) idcardno += dy[sum%11];
			return idcardno;
		}
		return idcardno;
	} catch(err){
		console.error(err.message);
	}
	return false;
}
//15位转18位身份证号  
let changeFivteenToEighteen = function(idcard)  
{  
	let ereg = '';
	if ((parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){ 
		ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性 
	} else { 
		ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性 
	} 
	return ereg.test(idcard); 
}; 

/**
 * [IsIdentity 严格的身份证号码检测函数]
 * @Author   szjcomo
 * @DateTime 2019-09-21
 * @param    {[type]}   idcardno [description]
 * @return   [bool/number] [true 正确 -1 身份证地区非法 -2 您输入的身份证号码有误 -3 身份证号码位数不对 -4 请查看控制台错误信息]
 */
exports.isIdentity = function(idcardno){
	try{
		let idcard_array 	= idcardno.split("");
		let JYM 			= "10X98765432";
		let area = {11 : "北京",12 : "天津",13 : "河北",14 : "山西",15 : "内蒙古",21 : "辽宁",22 : "吉林",23 : "黑龙江",31 : "上海",32 : "江苏",33 : "浙江",34 : "安徽",35 : "福建",36 : "江西",37 : "山东",41 : "河南",42 : "湖北",43 : "湖南",44 : "广东",45 : "广西",46 : "海南",50 : "重庆",51 : "四川",52 : "贵州",53 : "云南",54 : "西藏",61 : "陕西",62 : "甘肃",63 : "青海",64 : "宁夏",65 : "新疆",71 : "台湾",81 : "香港",82 : "澳门",91 : "国外"};
		if(idcardno.length == 15) return changeFivteenToEighteen(idcardno);
		let ereg = /^[1-9][0-9]{5}[1-2][(0|9)][0-9][0-9]((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式 
		if (parseInt(idcardno.substr(6, 4)) % 4 == 0
				|| (parseInt(idcardno.substr(6, 4)) % 100 == 0 && parseInt(idcardno
						.substr(6, 4)) % 4 == 0)) {
			ereg = /^[1-9][0-9]{5}[1-2][(0|9)][0-9][0-9]((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式 
		}
		if (ereg.test(idcardno)) {//测试出生日期的合法性 
			//计算校验位 
			let S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10]))
					* 7
					+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11]))
					* 9
					+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12]))
					* 10
					+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13]))
					* 5
					+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14]))
					* 8
					+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15]))
					* 4
					+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16]))
					* 2
					+ parseInt(idcard_array[7])
					* 1
					+ parseInt(idcard_array[8])
					* 6
					+ parseInt(idcard_array[9]) * 3;
			let Y 	= S % 11;
			let M 	= "F";
			M 		= JYM.substr(Y,1);//判断校验位
			if (area[parseInt(idcardno.substr(0, 2))] == null) {
				return false;
			}
			if (M == idcard_array[17]) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}		
	} catch(err){
		console.error(err.message);
	}
	return false;
}
